
# coding: utf-8
#%%
import os
from .processmethod import Process_method

#%%

########################################################T1像处理函数


class Bet_fast_T1_BF(Process_method):
    #作用原理：这是使用fsl的bet  fast命令对T1像进行预处理。并用slicesdir进行check bet即fast的运行结果，生成图片保存
    #参数说明：previous_filename是input file的full path
    #参数说明：所需参数在cmd_flag_parameter_list里面
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    #如：/media/root/Elements/XuanWu/T1_CP/subj_240_1_T1_CP.nii.gz,在本函数中，此文件并不会被自动生成建，而是在成功完成整个过程之后，用ln生成一个
    #soft link，这样可以用来作为此subj的T1像是否已经被处理过。避免重复处理。节约时间。
    
    #下面这些是为了slicesdir在当前工作目录产生的slicesdir/subj_24-_T1.png拷贝到次函数的输出文件夹。
    struct2mni_str = r'struct2mni'
    warpedstruct_str = 'warpedstruct'
    origin_dirname = 'origin'
    def concat_inputs(self):
        super().concat_inputs()
        
        #特有的文件路径
        self.origin_dirpath = self.top_dirpath + self.os_sep + self.subject + self.os_sep + self.origin_dirname
        if not os.path.exists(self.origin_dirpath):
            os.makedirs(self.origin_dirpath)
        self.bet_dirpath = self.top_dirpath + self.os_sep + self.subject + self.os_sep + self.bet_dirname
        if not os.path.exists(self.bet_dirpath):
            os.makedirs(self.bet_dirpath)
        self.fast_dirpath = self.top_dirpath + self.os_sep + self.subject + self.os_sep + self.fast_dirname
        if not os.path.exists(self.fast_dirpath):
            os.makedirs(self.fast_dirpath)
        self.fnirt_dirpath = self.top_dirpath + self.os_sep + self.subject + self.os_sep + self.fnirt_dirname
        if not os.path.exists(self.fnirt_dirpath):
            os.makedirs(self.fnirt_dirpath)
        
        #组合出bet用到的中间文件的full filepath
        self.origin_filepath = self.origin_dirpath + self.os_sep + os.path.split(self.previous_filename)[1]
        self.brain_filepath = self.bet_dirpath + self.os_sep + self.brain_str + self.end_part
        self.brainmask_filepath = self.bet_dirpath + self.os_sep +  self.brain_str + self.brainmask_str + self.end_part
        self.png_filepath = self.top_dirpath + self.os_sep + self.subject + self.underline + self.brain_str + self.png_str
        
        #组合出fast用到的中间文件的full filepath
        self.csf_filepath = self.fast_dirpath + self.os_sep + self.brain_str + self.csf_str + self.end_part
        self.gm_filepath = self.fast_dirpath + self.os_sep +  self.brain_str + self.gm_str + self.end_part
        self.wm_filepath = self.fast_dirpath + self.os_sep +  self.brain_str + self.wm_str + self.end_part
        self.basename_filepath = self.fast_dirpath + self.os_sep + self.brain_str + self.end_part
        self.csf_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.underline + self.csf_str + self.png_str
        self.gm_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.underline + self.gm_str + self.png_str
        self.wm_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.underline + self.wm_str + self.png_str
        
        #组合出fast用到的中间文件的full filepath
        self.struct2mni_filepath = self.fnirt_dirpath + self.os_sep + self.struct2mni_str + self.mat_str
        self.warpedstruct_filepath = self.fnirt_dirpath + self.os_sep +  self.warpedstruct_str + self.end_part
        self.warpedstruct_png_filepath  = self.top_dirpath + self.os_sep + self.subject + self.underline + self.warpedstruct_str + self.png_str
        self.warpstruct2mni_filepath = self.fnirt_dirpath + self.os_sep +  self.warpstruct2mni_str + self.end_part
    def concat_script(self):
        
        #cmd_0的内容：cp  self.previous_filename  self.origin_filepath
        cmd_1 = r'cp  ' + self.previous_filename + self.space + self.origin_filepath
        ####开始bet
        #使用bet做brain extraction
        #cmd_1的内容：bet  subj_240_8_T1.nii.gz   subj_240_8_T1_BF-brain.nii.gz  -R 
        cmd_2 = r'bet  ' + self.origin_filepath + self.space + self.brain_filepath + r'  -R  -m '
        #使用slicesdir生成用于检测brain extraction效果的png图片
        #cmd_3的内容：slicesdir  -p   subj_240_8_T1_BF_brain.nii.gz    subj_240_1_T1.nii.gz  
        cmd_3 = r'cd ' + self.bet_dirpath + self.cmd_sep + r'slicesdir  -p ' + self.brain_filepath  + self.space + self.origin_filepath
        #将slicesdir生成的png图片从工作目录转移至次函数的输出文件夹
        #cmd_3a的内容：mv  original_png_filepath  png_filepath
        original_png_filepath = self.bet_dirpath  + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        cmd_3a = r'mv ' + original_png_filepath  + self.space + self.png_filepath
        
        ####用fast多上一步生成的brain_filepath文件做segment，顺表做了bias correction，最终得到WM GM CSF各自的mask
        #cmd_5的内容：fast  -n 3  -t 1 -g  -B  subj_240_8_T1_BF-brain.nii.gz && 参数的意义：3中class，T1像，每种class一个mask，输出bias corrected image
        cmd_5 = r'fast  -n 3  -t 1 -g  -B -o ' + self.basename_filepath + self.space + self.brain_filepath
        #cmd_6的内容：slicesdir  -p   subj_240_8_T1_BF_brain_seg_0.nii.gz   subj_240_8_T1_BF-brain.nii.gz  
        cmd_6= r'cd ' + self.fast_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.csf_filepath + self.space + self.origin_filepath
        
        #cmd_6a的内容：mv  original_png_filepath  csf_png_filepath  
        original_png_filepath = self.fast_dirpath  + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        cmd_6a= r'mv   ' + original_png_filepath + self.space + self.csf_png_filepath
        
        #cmd_7的内容：slicesdir  -p   subj_240_8_T1_BF_brain_seg_1.nii.gz    subj_240_8_T1_BF-brain.nii.gz  
        cmd_7= r'cd ' + self.fast_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.gm_filepath + self.space + self.origin_filepath
    
        #cmd_7a的内容：mv  original_png_filepath  gm_png_filepath  
        original_png_filepath = self.fast_dirpath  + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        cmd_7a= r'mv   ' + original_png_filepath + self.space + self.gm_png_filepath
        
        #cmd_8的内容：slicesdir  -p   subj_240_8_T1_BF_brain_seg_2.nii.gz    subj_240_8_T1_BF-brain.nii.gz  
        cmd_8= r'cd ' + self.fast_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.wm_filepath + self.space + self.origin_filepath
        
        #cmd_8a的内容：mv  original_png_filepath  wm_png_filepath  
        original_png_filepath = self.fast_dirpath  + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        cmd_8a= r'mv   ' + original_png_filepath + self.space + self.wm_png_filepath
        
        ####将subj的T1像register到MNI 152  2*2*2    /usr/share/fsl/data/standard/MNI152_T1_2mm_brain.nii
        #cmd_9的内容：flirt -in subj_240_8_T1_brain.nii.gz   -ref ${FSL_DIR}/data/standard/MNI152_T1_2mm_brain.nii.gz  -omat test.mat 
        #-out test.nii.gz &&   注：-out可以省略
        cmd_9= r'flirt -in  ' + self.brain_filepath + r' -ref ' + self.mni152_2mm_brain_filepath + r' -omat ' + self.struct2mni_filepath
        #cmd_10的内容：fnirt --ref=${FSLDIR}/data/standard/MNI152_T1_2mm.nii.gz 
        #--refmask=${FSLDIR}/data/standard/MNI152_T1_2mm_brain_mask.nii.gz  
        #--in=/media/root/Elements3/XuanWu_gz/T1/subj_240_10_T1.nii.gz  --inmask=brain_mask.nii.gz 
        #--aff=/media/root/Elements3/XuanWu_gz/T1_BF/subj_240_10/fnirt/struct2mni.mat    
        #--cout=/media/root/Elements3/XuanWu_gz/T1_BF/subj_240_10/fnirt/warp_struct2std.nii.gz  
        #--iout=/media/root/Elements3/XuanWu_gz/T1_BF/subj_240_10/fnirt/warpedstruct.nii.gz   &&  
        #cmd_10= r'fnirt --ref=' + mni152_2mm_filepath + r' --refmask=' + mni152_2mm_brain_mask_filepath  + \
                        #r' --in=' + previous_filename  + r' --inmask=' + brainmask_filepath + \
                        #r' --cout='  +  warpstruct2mni_filepath  + r' --iout=' + warpedstruct_filepath + \
                        #r' --aff=' + struct2mni_filepath + cmd_sep    
        cmd_10 = r'fnirt --ref=' + self.mni152_2mm_filepath + r' --refmask=' + self.mni152_2mm_brain_mask_filepath \
                    +  r' --in=' + self.origin_filepath + r' --inmask=' + self.brainmask_filepath + r' --aff=' \
                    + self.struct2mni_filepath +  r'  --cout=' + self.warpstruct2mni_filepath + r' --iout=' + self.warpedstruct_filepath
    
        #cmd_11的内容：slicesdir  -p   subj_240_8_T1_BF_brain_seg_2.nii.gz    subj_240_8_T1_BF-brain.nii.gz  
        cmd_11= r'cd ' + self.fnirt_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.warpedstruct_filepath + self.space + self.mni152_2mm_filepath
    
        #cmd_11a的内容：mv  original_png_filepath  warpedstruct_png_filepath
        original_png_filepath = self.fnirt_dirpath  + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        cmd_11a= r'mv   ' + original_png_filepath + self.space + self.warpedstruct_png_filepath
        
        #cmd_12的内容 ln -s  warpedstruct_filepath  present_filename  
        cmd_12= r' ln -s ' + self.warpedstruct_filepath + self.space + self.present_filename
        
        
        self.shell_script = cmd_1 + self.cmd_sep + cmd_2  + self.cmd_sep +  cmd_3  + self.cmd_sep  +  cmd_3a  + self.cmd_sep  +  cmd_5  + self.cmd_sep  +  cmd_6  \
                            + self.cmd_sep  + cmd_6a  + self.cmd_sep  +  cmd_7  + self.cmd_sep  + cmd_7a  + self.cmd_sep  + cmd_8  + self.cmd_sep \
                            + cmd_8a  + self.cmd_sep  + cmd_9  + self.cmd_sep  + cmd_10  + self.cmd_sep  + cmd_11  + self.cmd_sep  + cmd_11a  \
                            + self.cmd_sep  + cmd_12
#        self.shell_script = cmd_1 +self.cmd_sep + cmd_2


#%%
# #################################################################PET像处理函数

class Bet_flirt_applywarp_pet_BFAP(Process_method):
    #作用原理：用bet flirt applywarp处理PET ASL CBF,对齐进行normalization
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    #elements string for filrpath
    brian_str = '_brain'
    flirt_brain_str = '_flirt_brain'
    flirt_mat_str = r'_flirt.mat'
    warped_str = '_warped'
    warped_brain_str = r'_warped_brain'
        
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        #get the filepath
        self.brain_filepath =  self.front_part + self.brian_str + self.end_part
        self.flirt_brain_filepath = self.front_part + self.flirt_brain_str + self.end_part
        self.flirt_mat_filepath =  self.front_part + self.flirt_mat_str
        self.warped_brain_filepath =  self.front_part + self.warped_brain_str + self.end_part
        self.subject_dirpath = self.top_dirpath + self.os_sep + self.subject   #only used for slicesdir  and will be delete later
        if not os.path.exists(self.subject_dirpath):
            os.makedirs(self.subject_dirpath)
        self.flirt_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.flirt_brain_str + self.png_str
        self.applywarp_png_filepath = self.top_dirpath + self.os_sep + self.subject +self.warped_str + self.png_str
        
        
    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。
        
        #cmd_2的内容：bet subj_240_8_T1.nii.gz   subj_240_8_T1_BF-brain.nii.gz   -R
        self.cmd_1 = r' bet ' + self.previous_filename  + self.space + self.brain_filepath  + r' -R '
        
        ##cmd_2 = r'flirt -in  ' + brain_filepath + r' -ref ' + mni152_2mm_brain_filepath + r' -omat ' + struct2mni_filepath 
        #cmd_2的内容：flirt -in func.nii -ref struct.nii  -omat func2struct.mat     brain_T1_filepath
        #flirt -in subj_240_1_BOLD_DFV-10_DS_ST-seqplus_RBFA_single_brain.nii.gz  -ref /media/root/Elements3/XuanWu_gz/T1_BF/subj_240_1/bet/brain.nii.gz  -omat aaa.mat  -out aaa.nii.gz
        self.cmd_2 =  r' flirt -in ' + self.brain_filepath  +  ' -ref  ' + self.brain_T1_filepath +  ' -omat ' + self.flirt_mat_filepath + ' -out ' +  self.flirt_brain_filepath

        #cmd_2的内容：slicesdir  -p   brain_filepath    brain_filepath
        self.cmd_3= r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.flirt_brain_filepath + self.space + self.brain_T1_filepath 

        #cmd_3a的内容：mv  original_png_filepath  wm_png_filepath 
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        self.cmd_3a= r'mv   ' + self.original_png_filepath + self.space + self.flirt_png_filepath

        #cmd_4的内容：applywarp --in=func.nii --premat=func2struct.mat --warp=warp_struct2mni.nii --ref=MNI152_T1_2mm.nii 
        #cmd_4 = r'applywarp --ref=' + mni152_2mm_brain_filepath + r' --in=' + brain_filepath + \
                                                                  #r' --out=' + warped_brain_filepath + \
                                                                  #r' --warp=' + warped_brain_filepath + \
                                                                  #r' --premat=' + flirt_mat_filepath   

        self.cmd_4 = r' applywarp --ref=' + self.mni152_2mm_filepath + ' --in=' + self.previous_filename + ' --out=' + self.warped_brain_filepath + '  --warp=' + self.warpstruct2mni_filepath +  ' --premat=' + self.flirt_mat_filepath

        #cmd_4的内容：slicesdir  -p   brain_filepath    brain_filepath
        self.cmd_5= r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.warped_brain_filepath + self.space + self.mni152_2mm_brain_filepath
        
        #cmd_5a的内容：mv  original_png_filepath  wm_png_filepath 
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep + self.T1_str + self.png_str
        self.cmd_5a= r'mv   ' + self.original_png_filepath + self.space + self.applywarp_png_filepath + self.cmd_sep + r' rm -rf ' + self.subject_dirpath

        #cmd_9的内容 ln -s   warped_brain_filepath  present_filename  
        self.cmd_6= r' ln -s ' +  self.warped_brain_filepath + self.space + self.present_filename

        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_3a + self.cmd_sep + self.cmd_4 + self.cmd_sep +  self.cmd_5 + self.cmd_sep + self.cmd_5a + self.cmd_sep + self.cmd_6


#%%

class Bet_flirt_applywarp_cbf_BFAC(Process_method):
    #作用原理：用bet flirt applywarp处理PET ASL CBF,对齐进行normalization
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    #elements string for filrpath
    pd_str = '_PD'
    cbf_str = '*CBF*'
    brian_str = '_brain'
    brian_pd_str = '_brain_pd'
    flirt_brain_str = '_flirt_brain'
    flirt_brain_pd_str = '_flirt_brain_pd'
    flirt_mat_str = r'_flirt.mat'
    flirt_mat_pd_str = '_flirt_pd.mat'
    concat_mat_str = r'_concat.mat'
    warped_str = '_warped'
    warped_brain_str = r'_warped_brain'
        
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        #get the filepath
        self.pd_dirpath = self.temp2_dir
        self.pd_filepath = self.pd_dirpath + self.os_sep + os.path.split(self.previous_filename)[1].replace(self.modality,self.pd_str)
        self.brain_filepath =  self.front_part + self.brian_str + self.end_part
        self.brain_pd_filepath =  self.front_part + self.brian_pd_str + self.end_part
        self.flirt_brain_filepath = self.front_part + self.flirt_brain_str + self.end_part
        self.flirt_brain_pd_filepath = self.front_part + self.flirt_brain_pd_str + self.end_part
        self.flirt_mat_filepath =  self.front_part + self.flirt_mat_str
        self.flirt_mat_pd_filepath =  self.front_part + self.flirt_mat_pd_str
        self.concat_mat_filepath = self.front_part + self.concat_mat_str
        self.warped_brain_filepath =  self.front_part + self.warped_brain_str + self.end_part
        self.subject_dirpath = self.top_dirpath + self.os_sep + self.subject   #only used for slicesdir  and will be delete later
        if not os.path.exists(self.subject_dirpath):
            os.makedirs(self.subject_dirpath)
        self.flirt_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.flirt_brain_str + self.png_str
        self.flirt_png_pd_filepath = self.top_dirpath + self.os_sep + self.subject + self.flirt_brain_pd_str + self.png_str
        self.applywarp_png_filepath = self.top_dirpath + self.os_sep + self.subject +self. warped_str + self.png_str

    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。

        #cmd_1的内容：bet subj_240_8_T1.nii.gz   subj_240_8_T1_BF-brain.nii.gz   -R
        self.cmd_1 = r' bet ' + self.previous_filename  + self.space + self.brain_filepath + r' -R '
        #cmd_1a的内容：bet subj_240_8_T1.nii.gz   subj_240_8_T1_BF-brain.nii.gz   -R
        self.cmd_2 = r' bet ' + self.pd_filepath  + self.space + self.brain_pd_filepath + r' -f 0.8 -R '
        
        self.cmd_3 = r' flirt -in ' + self.brain_filepath  +  ' -ref  ' + self.brain_pd_filepath +  ' -omat ' + self.flirt_mat_filepath + ' -out ' +  self.flirt_brain_filepath
        
        #cmd_3a的内容：mv  original_png_filepath  wm_png_filepath 
        self.cmd_3a= r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.brain_pd_filepath + self.space + self.flirt_brain_filepath
        
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep + self.cbf_str + self.png_str
        self.cmd_3b= r'mv   ' + self.original_png_filepath + self.space + self.flirt_png_filepath
        
        
        ##cmd_2 = r'flirt -in  ' + brain_filepath + r' -ref ' + mni152_2mm_brain_filepath + r' -omat ' + struct2mni_filepath 
        #cmd_2的内容：flirt -in func.nii -ref struct.nii  -omat func2struct.mat     brain_T1_filepath
        #flirt -in subj_240_1_BOLD_DFV-10_DS_ST-seqplus_RBFA_single_brain.nii.gz  -ref /media/root/Elements3/XuanWu_gz/T1_BF/subj_240_1/bet/brain.nii.gz  -omat aaa.mat  -out aaa.nii.gz
        self.cmd_4 =  r' flirt -in ' + self.brain_pd_filepath  +  ' -ref  ' + self.brain_T1_filepath +  ' -omat ' + self.flirt_mat_pd_filepath + ' -out ' +  self.flirt_brain_pd_filepath
        
        #cmd_2的内容：slicesdir  -p   brain_filepath    brain_filepath
        self.cmd_4a= r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.brain_T1_filepath  + self.space + self.flirt_brain_pd_filepath
        #cmd_3a的内容：mv  original_png_filepath  wm_png_filepath 
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep + self.cbf_str + self.png_str
        self.cmd_4b= r'mv   ' + self.original_png_filepath + self.space + self.flirt_png_pd_filepath

        #cmd_3的内容：convert_xfm  -omat  self.concat_mat_filepath  -concat  self.pd_flirt_mat_filepath   self.flirt_mat_filepath
        self.cmd_5 =  r' convert_xfm  -omat ' + self.concat_mat_filepath +  ' -concat ' + self.flirt_mat_pd_filepath  + self.space + self.flirt_mat_filepath
        
        #cmd_4的内容：applywarp --in=func.nii --premat=func2struct.mat --warp=warp_struct2mni.nii --ref=MNI152_T1_2mm.nii 
        #cmd_4 = r'applywarp --ref=' + mni152_2mm_brain_filepath + r' --in=' + brain_filepath + \
                                                                  #r' --out=' + warped_brain_filepath + \
                                                                  #r' --warp=' + warped_brain_filepath + \
                                                                  #r' --premat=' + flirt_mat_filepath   

        self.cmd_6 = r' applywarp --ref=' + self.mni152_2mm_filepath + ' --in=' + self.previous_filename + ' --out=' + self.warped_brain_filepath + '  --warp=' + self.warpstruct2mni_filepath +  ' --premat=' + self.concat_mat_filepath

        #cmd_4的内容：slicesdir  -p   brain_filepath    brain_filepath
        self.cmd_6a= r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.mni152_2mm_brain_filepath + self.space + self.warped_brain_filepath
        
        #cmd_5a的内容：mv  original_png_filepath  wm_png_filepath 
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep + self.cbf_str + self.png_str
        self.cmd_6b= r'mv   ' + self.original_png_filepath + self.space + self.applywarp_png_filepath + self.cmd_sep + r' rm -rf ' + self.subject_dirpath

        #cmd_9的内容 ln -s   warped_brain_filepath  present_filename  
        self.cmd_7= r' ln -s ' +  self.warped_brain_filepath + self.space + self.present_filename

        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_3a  + self.cmd_sep \
                                            + self.cmd_3b + self.cmd_sep + self.cmd_4 + self.cmd_sep + self.cmd_4a + self.cmd_sep + self.cmd_4b\
                                            + self.cmd_sep +  self.cmd_5 + self.cmd_sep + self.cmd_6 + self.cmd_sep + self.cmd_6a \
                                            + self.cmd_sep + self.cmd_6b + self.cmd_sep + self.cmd_7
        
#        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2
#%%

class Delete_firstN_volumes_DFV(Process_method):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令删除前n个时间点，n的取值在parameter里面
    #参数说明:use input_list instead of the original 4 parameters which explained below to ease the use of 'pool.map()' in agile_pipe().
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list里的元素表示删除几个时间点
    #参数说明：present_filename是output file的full path
    #参数说明：由于类似于-a这种flag的parameter很复杂，需要专门写一组判断，如果是这种有复杂参数的flag，则专门调用一个函数来进行解析，其他的类似于'-prefix'这种简单的flag就
    #参数说明：直接在for key in cmd_flag_dict内部用str的+运算符连接起来即可。且用list.append()方法不循环中组合的flag+parameter存放起来。循环结束后，再用list表达式
    #参数说明：将afni命令和list的各个元素连接起来，即构成可执行的cmd。这种解析方式，可以在在manipulate_methods这个dict中较为随意。即可以完全通过此代码随意的调用shell命令。
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    temp_str = r'_temp'
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        self.numb_unneeded_volumes = self.cmd_flag_parameter_list[0]
        self.tem_img_filepath = self.front_part + self.temp_str + self.end_part

    def concat_script(self):
        #cmd_1的内容：3dcalc -a ${rest}.nii.gz -expr 'a' -prefix ${rest}_dr.nii.gz  -datum float
        self.cmd_1 =  r'3dcalc -a ' + self.previous_filename + r" -expr 'a' -prefix " + self.tem_img_filepath  + r' -datum float  '  

        #cmd_2的内容：3dTcat -prefix subj_240_9_BOLD_tcat.nii.gz subj_240_9_BOLD.nii.gz[10..$]
        self.cmd_2 = r'3dTcat -prefix ' + self.present_filename  + ' \'' + self.tem_img_filepath + r'[' +  self.numb_unneeded_volumes + r'..' + r'$]' + '\'' 

        #cmd_3的内容：rm  -f  tem_img_filepath
        self.cmd_3 = r'rm  -f   ' + self.tem_img_filepath

        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3


#%%


class Despike_DS(Process_method):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令dispike
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list=[],因为dispike不需要参数
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：import subprocess   import nibabel as nib     import numpy as np 
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    def concat_script(self):
        #cmd_1的内容：3dDespike -NEW -nomask -prefix pb01.$subj.r$run.despike   pb00.$subj.r$run.tcat+orig
        self.cmd_1 = r'3dDespike -NEW -nomask  -prefix ' + self.present_filename + self.space + self.previous_filename
        self.shell_script = self.cmd_1 


#%%


class Slice_timing_ST(Process_method):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令做slice timing。
    #参数说明：previous_filename是input file的full path
    #参数说明：所需参数在cmd_flag_parameter_list里面
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    #modality = '_BOLD'
    def concat_inputs(self):
        super().concat_inputs()
        self.tpattern = self.cmd_flag_parameter_list[0]
        self.tzero = '0'

    def concat_script(self):
        #cmd_1的内容：3dTshift -tzero 0 -quintic  -tpattern  ${tpattern} -prefix pb02.$subj.r$run.tshift   pb01.$subj.r$run.despike+orig
        self.cmd_1 = r'3dTshift  -tzero ' + self.tzero + ' -quintic ' + r' -tpattern ' + self.tpattern +                                                              r' -prefix ' + self.present_filename + self.space + self.previous_filename      
        self.shell_script = self.cmd_1


#%%


class Realign_RA(Process_method):
    #作用原理：同时进行realign bet flirt applywarp，此函数必须与bet_fast()搭配使用，先用bet_fast处理T1像，然后可以使用此函数处理BOLD像
    #参数说明：previous_filename是input file的full path
    #参数说明：所需参数在cmd_flag_parameter_list里面
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    #modality = '_BOLD'
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        #get the filepath
        self.numb_static_volume = self.cmd_flag_parameter_list[0]
        self.realign_param_filepath = self.front_part + self.realign_param_str

    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。
        #cmd_1的内容：    3dvolreg  -zpad 1 -base vr_base+orig -1Dfile dfile.r$run.1D -prefix rm.epi.volreg.r$run -cubic                                             \
        #                        -1Dmatrix_save mat.r$run.vr.aff12.1D  pb02.$subj.r$run.tshift+orig
        #cmd_1的内容：3dvolreg -Fourier -twopass -base ${rest}_ro_mean.nii.gz -zpad 4 -prefix ${rest}_mc.nii.gz 
        #                                                   -1Dfile ${rest}_mc.1D ${rest}_ro.nii.gz
        #cmd_1的内容：3dvolreg -zpad 1 -cubic  -base 'realign_fmri_filepath[2]'  -prefix ${rest}_mc.nii.gz 
        #                                                   -1Dfile ${rest}_mc.1D ${rest}_ro.nii.gz
        
        
        self.cmd_1 =  r' 3dvolreg -zpad 1 -cubic  -base ' + ' \'' + self.previous_filename + r'[' + self.numb_static_volume + r']' + '\''  +  r'  -prefix ' + self.present_filename +  r' -1Dfile ' + self.realign_param_filepath + self.space + self.previous_filename
        
        self.shell_script = self.cmd_1


#%%

class Coregister_flirt_bold_CRFB(Process_method):
    #作用原理：同时进行realign bet flirt applywarp，此函数必须与bet_fast()搭配使用，先用bet_fast处理T1像，然后可以使用此函数处理BOLD像
    #参数说明：previous_filename是input file的full path
    #参数说明：所需参数在cmd_flag_parameter_list里面
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    #modality = '_BOLD'
    single_fmri_str = '_single'
    brain_single_fmri_str = '_brain_single'
    coregister_brain_single_fmri_str = '_coregister_brain_single'
 
    def concat_inputs(self):
        super().concat_inputs()
        self.numb_static_volume = self.cmd_flag_parameter_list[0]
        self.fract_intensity_thres = self.cmd_flag_parameter_list[1]
        
        self.single_fmri_filepath =  self.front_part + self.single_fmri_str + self.end_part
        self.brain_single_fmri_filepath = self.front_part + self.brain_single_fmri_str + self.end_part
        self.coregister_mat_filepath =  self.front_part + self.coregister_mat_str
        self.coregister_brain_single_fmri_filepath = self.front_part + self.coregister_brain_single_fmri_str + self.end_part
        self.subject_dirpath = self.top_dirpath + self.os_sep + self.subject   #only used for slicesdir  and will be delete later
        if not os.path.exists(self.subject_dirpath):
            os.makedirs(self.subject_dirpath)
        self.coregister_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.coregister_brain_single_fmri_str + self.png_str
        
    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。
        
        #cmd_1的内容：3dbucket -prefix subj_240_9_BOLD_brain_single.nii.gz 'subj_240_9_BOLD.nii.gz[2]'
        self.cmd_1 = r' 3dbucket -prefix ' + self.single_fmri_filepath + ' \'' + self.previous_filename  + r'[' + self.numb_static_volume + r']'  + '\''   
            
        #cmd_2的内容：bet subj_240_8_T1.nii.gz   subj_240_8_T1_BF-brain.nii.gz
        self.cmd_2= r' bet ' + self.single_fmri_filepath  + self.space + self.brain_single_fmri_filepath  + ' -f ' + self.fract_intensity_thres
        
        ##cmd_3 = r'flirt -in  ' + brain_filepath + r' -ref ' + mni152_2mm_brain_filepath + r' -omat ' + struct2mni_filepath 
        #cmd_3的内容：flirt -in func.nii -ref struct.nii  -omat func2struct.mat     brain_T1_filepath
        #flirt -in subj_240_1_BOLD_DFV-10_DS_ST-seqplus_RBFA_single_brain.nii.gz  
        #-ref /media/root/Elements3/XuanWu_gz/T1_BF/subj_240_1/bet/brain.nii.gz  -omat aaa.mat  -out aaa.nii.gz
        self.cmd_3 =  r' flirt -in ' + self.brain_single_fmri_filepath  +  ' -ref  ' + self.brain_T1_filepath +  ' -omat ' + self.coregister_mat_filepath + ' -out ' +  self.coregister_brain_single_fmri_filepath
        
        #cmd_4的内容：slicesdir  -p   brain_fmri_filepath    brain_filepath
        self.cmd_4 = r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p  ' +  self.brain_T1_filepath  + self.space + self.coregister_brain_single_fmri_filepath
        
        #cmd_4a的内容：mv  original_png_filepath  wm_png_filepath 
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep +  self.subject_stars + self.png_str
        self.cmd_4a= r'mv   ' + self.original_png_filepath + self.space + self.coregister_png_filepath +   self.cmd_sep + r' rm -rf ' + self.subject_dirpath
        
        #cmd_5:ln -s  brain_fmri_filepath  present_filename
        self.cmd_5 = r' ln -s ' + self.previous_filename + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_4 +  self.cmd_sep + self.cmd_4a + self.cmd_sep + self.cmd_5


#%%
        
class Coregister_bbresiger_bold_CRBB(Process_method):
    pass

#%%
##%%
#    
#class Coregister_flirt_cbf_CRFC(Process_method):
#    pass
#
##%%
#    
#class Coregister_bbresiger_cbf_CRBC(Process_method):
#    pass    
#
##%%
#    
#class Coregister_flirt_pet_CRFP(Process_method):
#    pass
#
##%%
#    
#class Coregister_bbresiger_pet_CRBP(Process_method):
#    pass    

#%%
    
class Motion_parameters_regress_MPR(Process_method):
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        self.numb_unneeded_volumes = self.cmd_flag_parameter_list[0]
        self.tem_img_filepath = self.front_part + self.temp_str + self.end_part

    def concat_script(self):
        #cmd_1的内容：3dcalc -a ${rest}.nii.gz -expr 'a' -prefix ${rest}_dr.nii.gz  -datum float
        self.cmd_1 =  r'3dcalc -a ' + self.previous_filename + r" -expr 'a' -prefix " + self.tem_img_filepath  + r' -datum float  '  

        #cmd_2的内容：3dTcat -prefix subj_240_9_BOLD_tcat.nii.gz subj_240_9_BOLD.nii.gz[10..$]
        self.cmd_2 = r'3dTcat -prefix ' + self.present_filename  + ' \'' + self.tem_img_filepath + r'[' + self.numb_unneeded_volumes + r'..' + r'$]' + '\'' 

        #cmd_3的内容：rm  -f  tem_img_filepath
        self.cmd_3 = r'rm  -f   ' + self.tem_img_filepath

        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3

#%%

class White_ventricle_regress_WVR(Process_method):
    
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        self.numb_unneeded_volumes = self.cmd_flag_parameter_list[0]
        self.tem_img_filepath = self.front_part + self.temp_str + self.end_part

    def concat_script(self):
        #cmd_1的内容：3dcalc -a ${rest}.nii.gz -expr 'a' -prefix ${rest}_dr.nii.gz  -datum float
        self.cmd_1 =  r'3dcalc -a ' + self.previous_filename + r" -expr 'a' -prefix " + self.tem_img_filepath  + r' -datum float  '  

        #cmd_2的内容：3dTcat -prefix subj_240_9_BOLD_tcat.nii.gz subj_240_9_BOLD.nii.gz[10..$]
        self.cmd_2 = r'3dTcat -prefix ' + self.present_filename  + ' \'' + self.tem_img_filepath + r'[' +  self.numb_unneeded_volumes + r'..' + r'$]' + '\'' 

        #cmd_3的内容：rm  -f  tem_img_filepath
        self.cmd_3 = r'rm  -f   ' + self.tem_img_filepath

        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3

#%%
        
class Covariance_regress_CVR(Process_method):
    pass

#%%
class Detrend_DT(Process_method):
    #作用原理：。
    #参数说明：previous_filename是input file的full path
    #参数说明：无需参数，故cmd_flag_parameter_list=[]
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    #test if the input_list has four parameters

    #basic string
    mean_str = '-mean'
    dt_str = '-dt'
    
    def concat_inputs(self):
        self.mean_filepath = self.front_part + self.mean_str + self.end_part
        self.dt_filepath = self.front_part + self.dt_str + self.end_part
    def concat_script(self):   
        #cmd_1的内容：3dTstat -mean -prefix ${FC_dir}/${rest}_filt_mean.nii.gz ${FC_dir}/${rest}_filt.nii.gz
        cmd_1 = r'3dTstat -mean -prefix ' + self.mean_filepath  + self.space + self.previous_filename
        
        #cmd_2的内容：3dDetrend -polort 2 -prefix ${FC_dir}/${rest}_dt.nii.gz ${FC_dir}/${rest}_filt.nii.gz
        cmd_2 = r'3dDetrend -polort 2 -prefix ' + self.dt_filepath  + self.space + self.previous_filename
        
        #cmd_3的内容：3dcalc -a ${FC_dir}/${rest}_filt_mean.nii.gz -b ${FC_dir}/${rest}_dt.nii.gz -expr 'a+b' -prefix ${FC_dir}/${rest}_pp_sm0.nii.gz
        cmd_3 = r'3dcalc -a ' + self.mean_filepath  + r' -b ' + self.dt_filepath + ' \'a+b\' -prefix '  +  self.present_filename
        
        self.shell_script = cmd_1 + self.cmd_sep + cmd_2 + self.cmd_sep + cmd_3

#%%
class Filt_FL(Process_method):
    #作用原理：
    #参数说明：previous_filename是input file的full path
    #参数说明：无需参数，故cmd_flag_parameter_list=[]
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    #test if the input_list has four parameters
    def concat_inputs(self):
        self.lowpass = self.cmd_flag_parameter_list[0]
        self.highpass = self.cmd_flag_parameter_list[0]
    def concat_script(self):   
    #cmd_1的内容：3dFourier -lowpass ${lp} -highpass ${hp} -retrend -prefix ${FC_dir}/${rest}_filt.nii.gz ${FC_dir}/${rest}_res.nii.gz
        cmd_1 = r'3dFourier -lowpass ' + self.lowpass + r' -highpass ' + self.highpass + r' -retrend -prefix '  + self.present_filename  + self.space + self.previous_filename  
        self.shell_script = cmd_1
#%%

class Resample_RS(Process_method):

    #self.brainMask_filepath_gz 此参数应该有user任意指定
    def concat_inputs(self):
        if self.temp1_dir != '':
            self.brainMask_filepath_gz = self.temp1_dir
    def concat_script(self):   
        #r'/root/Documents/BrainMask_05_61x73x61.nii.gz'
        #cmd_1的内容：3dresample -master master+orig -prefix new.dset -input old+orig
        cmd_1 = r'3dresample -master ' + self.brainMask_filepath_gz + r' -prefix ' + self.present_filename + r' -input '  + self.previous_filename
        self.shell_script = cmd_1


#%%
      
class Reorient_RO(Process_method):
    #3dresample
      pass

#%%

class Normalize_MNI__NMNI(Process_method):
    #作用原理：同时进行realign bet flirt applywarp，此函数必须与bet_fast()搭配使用，先用bet_fast处理T1像，然后可以使用此函数处理BOLD像
    #参数说明：previous_filename是input file的full path
    #参数说明：所需参数在cmd_flag_parameter_list里面
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    warped_brain_fmri_str = r'_warped_brain'
    single_warped_brain_fmri_str = r'_single_warped_brain'
    warpedfmri_str = '_warpedfmri'

    def concat_inputs(self):
        super().concat_inputs()
        self.process_class_suffix = self.underline + self.cmd_flag_parameter_list[0].upper() #the 'nm' should be changed to up case 'NM'
        self.numb_static_volume = self.cmd_flag_parameter_list[1]
        
        #because there are many coregisteration methods(affine/bbregister) to be used,so coregistration_class_str is assigned according to the
        # method that is used in the specific pipeline. this makes the Normalization_NM class compatiable with arbitrary coregistration class.
        self.position = self.present_dirname.rfind(self.process_class_suffix)
        self.coregister_mat_filepath = self.toptop_dir_path + self.os_sep + self.present_dirname[0:self.position] + self.os_sep + self.subject + self.underline + self.present_dirname[0:self.position] + self.coregister_mat_str
        #the expression above is used to concatenate the coregister mat filepath
        print('coregister_mat_filepath:',self.coregister_mat_filepath)
        if not os.path.exists(self.coregister_mat_filepath):
            raise Exception("you must have run Coregistration*_CR*,before you run Normalization_NM ") 
            
        #the expression below is used to concatenate the filename that is used in this process method
        self.warped_brain_fmri_filepath =  self.front_part + self.warped_brain_fmri_str + self.end_part
        self.single_warped_brain_fmri_filepath =  self.front_part + self.single_warped_brain_fmri_str + self.end_part
        
        self.subject_dirpath = self.top_dirpath + self.os_sep + self.subject   #only used for slicesdir  and will be delete later
        if not os.path.exists(self.subject_dirpath):
            os.makedirs(self.subject_dirpath)
        self.applywarp_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.warpedfmri_str + self.png_str
        
    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。
        #cmd_1的内容：applywarp --in=func.nii --premat=func2struct.mat --warp=warp_struct2mni.nii --ref=MNI152_T1_2mm.nii 
        #cmd_1 = r'applywarp --ref=' + mni152_2mm_brain_filepath + r' --in=' + brain_fmri_filepath + \
                                                                  #r' --out=' + warped_brain_fmri_filepath + \
                                                                  #r' --warp=' + warped_brain_fmri_filepath + \
                                                                  #r' --premat=' + coregister_mat_filepath 

        self.cmd_1 = r' applywarp --ref=' + self.mni152_2mm_brain_filepath +  ' --in=' + self.previous_filename +  ' --out=' + self.warped_brain_fmri_filepath +   '  --warp=' + self.warpstruct2mni_filepath +  ' --premat=' + self.coregister_mat_filepath

        #cmd_7的内容：3dbucket -prefix subj_240_9_BOLD_brain_single.nii.gz 'subj_240_9_BOLD.nii.gz[2]'
        self.cmd_2 = r' 3dbucket -prefix ' + self.single_warped_brain_fmri_filepath + ' \'' + self.warped_brain_fmri_filepath  +   r'[' + self.numb_static_volume + r']' + '\''  
        
        #cmd_8的内容：slicesdir  -p   brain_fmri_filepath    brain_filepath
        self.cmd_3 = r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.mni152_2mm_brain_filepath +  self.space + self.single_warped_brain_fmri_filepath
        
        #cmd_3a的内容：mv  original_png_filepath  wm_png_filepath
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep +  self.subject_stars  + self.png_str
        self.cmd_3a= r'mv   ' + self.original_png_filepath + self.space + self.applywarp_png_filepath + self.cmd_sep + r' rm -rf ' +   self.subject_dirpath

        #cmd_4的内容 ln -s   warped_brain_fmri_filepath  present_filename
        self.cmd_4= r' ln -s ' +  self.warped_brain_fmri_filepath + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 +  self.cmd_sep + self.cmd_3a + self.cmd_sep + self.cmd_4

#%%
class Normalize_fsaverage__NFSA(Process_method):
    #作用原理：同时进行realign bet flirt applywarp，此函数必须与bet_fast()搭配使用，先用bet_fast处理T1像，然后可以使用此函数处理BOLD像
    #参数说明：previous_filename是input file的full path
    #参数说明：所需参数在cmd_flag_parameter_list里面
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    warped_brain_fmri_str = r'_warped_brain'
    single_warped_brain_fmri_str = r'_single_warped_brain'
    warpedfmri_str = '_warpedfmri'

    def concat_inputs(self):
        super().concat_inputs()
        self.process_class_suffix = self.underline + self.cmd_flag_parameter_list[0].upper() #the 'nm' should be changed to up case 'NM'
        self.numb_static_volume = self.cmd_flag_parameter_list[1]
        
        #because there are many coregisteration methods(affine/bbregister) to be used,so coregistration_class_str is assigned according to the
        # method that is used in the specific pipeline. this makes the Normalization_NM class compatiable with arbitrary coregistration class.
        self.position = self.present_dirname.rfind(self.process_class_suffix)
        self.coregister_mat_filepath = self.toptop_dir_path + self.os_sep + self.present_dirname[0:self.position] + self.os_sep + self.subject + self.underline + self.present_dirname[0:self.position] + self.coregister_mat_str
        #the expression above is used to concatenate the coregister mat filepath
        print('coregister_mat_filepath:',self.coregister_mat_filepath)
        if not os.path.exists(self.coregister_mat_filepath):
            raise Exception("you must have run Coregistration*_CR*,before you run Normalization_NM ") 
            
        #the expression below is used to concatenate the filename that is used in this process method
        self.warped_brain_fmri_filepath =  self.front_part + self.warped_brain_fmri_str + self.end_part
        self.single_warped_brain_fmri_filepath =  self.front_part + self.single_warped_brain_fmri_str + self.end_part
        
        self.subject_dirpath = self.top_dirpath + self.os_sep + self.subject   #only used for slicesdir  and will be delete later
        if not os.path.exists(self.subject_dirpath):
            os.makedirs(self.subject_dirpath)
        self.applywarp_png_filepath = self.top_dirpath + self.os_sep + self.subject + self.warpedfmri_str + self.png_str
        
    def concat_script(self):
        ###########################################################################构造命令、即脚本执行。
        #cmd_1的内容：applywarp --in=func.nii --premat=func2struct.mat --warp=warp_struct2mni.nii --ref=MNI152_T1_2mm.nii 
        #cmd_1 = r'applywarp --ref=' + mni152_2mm_brain_filepath + r' --in=' + brain_fmri_filepath + \
                                                                  #r' --out=' + warped_brain_fmri_filepath + \
                                                                  #r' --warp=' + warped_brain_fmri_filepath + \
                                                                  #r' --premat=' + coregister_mat_filepath 

        self.cmd_1 = r' applywarp --ref=' + self.mni152_2mm_brain_filepath +  ' --in=' + self.previous_filename +  ' --out=' + self.warped_brain_fmri_filepath +   '  --warp=' + self.warpstruct2mni_filepath +  ' --premat=' + self.coregister_mat_filepath

        #cmd_7的内容：3dbucket -prefix subj_240_9_BOLD_brain_single.nii.gz 'subj_240_9_BOLD.nii.gz[2]'
        self.cmd_2 = r' 3dbucket -prefix ' + self.single_warped_brain_fmri_filepath + ' \'' + self.warped_brain_fmri_filepath  +   r'[' + self.numb_static_volume + r']' + '\''  
        
        #cmd_8的内容：slicesdir  -p   brain_fmri_filepath    brain_filepath
        self.cmd_3 = r'cd ' + self.subject_dirpath + self.cmd_sep + r'slicesdir  -p   ' + self.mni152_2mm_brain_filepath +  self.space + self.single_warped_brain_fmri_filepath
        
        #cmd_3a的内容：mv  original_png_filepath  wm_png_filepath
        self.original_png_filepath = self.subject_dirpath + self.os_sep + self.slicesdir_str + self.os_sep +  self.subject_stars  + self.png_str
        self.cmd_3a= r'mv   ' + self.original_png_filepath + self.space + self.applywarp_png_filepath + self.cmd_sep + r' rm -rf ' +   self.subject_dirpath

        #cmd_4的内容 ln -s   warped_brain_fmri_filepath  present_filename
        self.cmd_4= r' ln -s ' +  self.warped_brain_fmri_filepath + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 +  self.cmd_sep + self.cmd_3a + self.cmd_sep + self.cmd_4

#%%
        
class Smooth_spatial__SMSP(Process_method):
    #作用原理：
    #参数说明：previous_filename是input file的full path
    #参数说明：无需参数，故cmd_flag_parameter_list=[]
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：r' --mask ' + self.brainmask_filename_gz 这个位置需要改进，此参数应该由user任意指定
    def concat_inputs(self):
        self.fwhm = self.cmd_flag_parameter_list[0]
        if self.temp1_dir != '':
            self.brainmask_filepath_gz = self.temp1_dir
    def concat_script(self):   
    #cmd_1的内容：mri_fwhm --i pp_sm0.nii.gz --o pp_sm${FWHM}.nii.gz --smooth-only --fwhm ${FWHM} --mask pp_mask.nii.gz
        cmd_1 = r'mri_fwhm --i ' + self.previous_filename + r' --o ' + self.present_filename + r' --smooth-only --fwhm '  + self.fwhm  + r' --mask ' + self.brainmask_filepath_gz
        self.shell_script = cmd_1
        

#%%
    
class Smooth_surface__SMSF(Process_method):
    pass


#%%


class Kcc_reho_KRH(Process_method):
    #作用原理：
    #参数说明：previous_filename是input file的full path
    #参数说明：无需参数，故cmd_flag_parameter_list=[]
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：
    def concat_script(self):
        #cmd_1的内容：3dReHo -inset subj_240_9_BOLD.nii.gz   -prefix reho.nii.gz  
        #+ ' -mask ' + self.mni152_2mm_brain_filepath 不在标准空间做了
        self.cmd_1 = r'3dReHo -inset ' + self.previous_filename + r' -prefix ' + self.present_filename 
        self.shell_script = self.cmd_1

#%%
        
class Kcc_reho_surface_KRHS(Process_method):
    pass


#%%

class Alff_ALFF(Process_method):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令删除前n个时间点，n的取值在parameter里面
    #参数说明:use input_list instead of the original 4 parameters which explained below to ease the use of 'pool.map()' in agile_pipe().
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list里的元素表示删除几个时间点
    #参数说明：present_filename是output file的full path
    #参数说明：由于类似于-a这种flag的parameter很复杂，需要专门写一组判断，如果是这种有复杂参数的flag，则专门调用一个函数来进行解析，其他的类似于'-prefix'这种简单的flag就
    #参数说明：直接在for key in cmd_flag_dict内部用str的+运算符连接起来即可。且用list.append()方法不循环中组合的flag+parameter存放起来。循环结束后，再用list表达式
    #参数说明：将afni命令和list的各个元素连接起来，即构成可执行的cmd。这种解析方式，可以在在manipulate_methods这个dict中较为随意。即可以完全通过此代码随意的调用shell命令。
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    #llf_str = r'_LLF'
    alff_str = r'_ALFF'
    falff_str = r'_fALFF'
    malff_str = r'_mALFF'
    
    #rsfa_str = r'_RSFA'
    #frsfa_str = r'_fRSFA'
    #mrsfa_str = r'_mRSFA'
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
       
        self.low_freq_band = self.cmd_flag_parameter_list[0]
        self.high_freq_band = self.cmd_flag_parameter_list[1]
        
        self.afni_alff_fmri_filepath = self.front_part  + self.alff_str + self.star
        self.alff_fmri_filepath = self.front_part + self.alff_str + self.end_part
        
        self.afni_falff_fmri_filepath = self.front_part  + self.falff_str + self.star
        self.falff_fmri_filepath = self.front_part + self.falff_str + self.end_part
        
        self.afni_malff_fmri_filepath = self.front_part  + self.malff_str + self.star
        self.malff_fmri_filepath = self.front_part + self.malff_str + self.end_part
        
    def concat_script(self):
        #cmd_1的内容：3dRSFC  -nodetrend   -prefix test  -mask  MNI152_T1_2mm_brain_mask.nii.gz   0.01 0.1  
        #/root/Documents/subj_240_1/subj_240_1_BOLD_DFV-10_DS_RA-2_ST-seqplus_CR-2-0.5_NM-nm-2_warped_brain.nii.gz 
        #r' -mask ' + self.mni152_2mm_brain_filepath
        self.cmd_1 =  r'3dRSFC  -nodetrend   -prefix ' + self.front_part + self.space +                                             self.low_freq_band + self.space + self.high_freq_band + self.space + self.previous_filename  

        #cmd_2的内容：3dAFNItoNIFTI -prefix abc_head.nii.gz   test_ALFF+tlrc.BRIK  
        self.cmd_2 = r'3dAFNItoNIFTI -prefix ' + self.alff_fmri_filepath  + self.space + self.afni_alff_fmri_filepath
        
        #cmd_3的内容：3dAFNItoNIFTI -prefix abc_head.nii.gz   test_ALFF+tlrc.BRIK  
        self.cmd_3 = r'3dAFNItoNIFTI -prefix ' + self.falff_fmri_filepath  + self.space + self.afni_falff_fmri_filepath

        #cmd_4的内容：3dAFNItoNIFTI -prefix abc_head.nii.gz   test_ALFF+tlrc.BRIK  
        self.cmd_4 = r'3dAFNItoNIFTI -prefix ' + self.malff_fmri_filepath  + self.space + self.afni_malff_fmri_filepath
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_4

#%%
class Alff_surface_ALFFS(Process_method): 
    pass

#%%
class Functional_connectivity_FC(Process_method):
    pass
#%%
class Functional_connectivity_surface_FCS(Process_method):
    pass